package com.tn.wservice;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.UUID;

import javax.xml.namespace.QName;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPMessage;

public class TestSOAPClient {

	public static void main(String[] args) throws Throwable {

		System.out.println("Jai Baberi");
		
		URL endPoint = new URL("http://localhost:8888/poservice");
		UUID randomUUID = UUID.randomUUID();
		System.out.println("Random UUID generated " + randomUUID.toString());
		
		MessageFactory factory = MessageFactory.newInstance();
		SOAPMessage soapMessage = factory.createMessage();

		SOAPBody soapBody = soapMessage.getSOAPBody();
		soapBody.addBodyElement(new QName(SOAPHandler.NAMESPACE_URI, "handleAttachment"));
		soapBody.setAttribute("ponumber", "PO123456789");
		
		AttachmentPart attachPart = soapMessage.createAttachmentPart();
		attachPart.setRawContent(new FileInputStream(new File("E://YSN_Doc_onwer.docx")), "application/msword");
		attachPart.setContentId(randomUUID.toString());
		
		soapMessage.addAttachmentPart(attachPart);

		SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
		SOAPConnection connection = connectionFactory.createConnection();
		// TODO Change this to point to correct URL
		SOAPMessage response = connection.call(soapMessage, endPoint);

		System.out.println("Program Completed");
	}

}
